<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/11 9:28
 * Desc:
 */
class Service_Api_Techan extends Contract_Api_Product
{
    /**
     * 类型ID
     */
    const TYPE_ID = 118;

    public function search(array $params = array()): array
    {
        $page     = Arr::get($params, 'page', 1);
        $pagesize = Arr::get($params, 'pagesize', 10);
        $keyword  = Arr::get($params, 'keyword', '');
        $mid  = Arr::get($params, 'mid', '');

        $data = Model_Api_V2_Techan::search($params, $keyword, $page, $pagesize);

        $filter_keys = "litpic,price,sellpoint,commentnum,iconlist,id,satisfyscore,sell_num,sellprice,status,title,attrlist,finaldest_name";

        if(!empty($data['list'])){
            // 获取营销策略
            $marketing_strategy = Model_Api_V2_Marketing_Strategy::get_instance(self::TYPE_ID, array('mid'=> $mid));

            foreach ($data['list'] as &$v) {
                // 封面
                $v['litpic'] = $this->img_url($v['litpic'], 400, 400);
                // 图标
                $v['iconlist'] = $this->icons($v['iconlist']);
                //属性列表.
                $v['attrlist'] = Model_Techan_Attr::get_attr_list($v['attrid'], self::TYPE_ID);
                // 最终目的地
                $v['finaldest_name'] = Model_Api_V2_System::get_finaldest_name($v['finaldestid']);
                // 最低价
                $v['price'] = Model_Techan::get_minprice($v['id'], $v);
                //只返回需要的数据
                $v = $this->filter_data($filter_keys, $v);
                // 营销策略
                $v['marketing_strategy'] = $marketing_strategy->get_support_application($v['id']);
            }
        }

        return array(
            'list' => $data['list'],
            'total' => $data['total'],
            'is_finished' => $data['is_finished']
        );
    }

    public function info(array $params = array()): array
    {
        $info = Model_Api_V2_Techan::detail($params['id']);

        $data = array(
            'id' => $info['id'],
            'description' => $info['description'],
            'sellpoint' => $info['sellpoint'],
            'status' => $info['status'],
            'title' => $info['title'],
            'ishidden' => $info['ishidden'],
        );

        // 最终目的地
        $data['finaldest_name'] = Model_Api_V2_System::get_finaldest_name($info['finaldestid']);
        //产品图片
        $piclist = Product::pic_list($info['piclist']);
        $p       = array();
        foreach ($piclist as &$pic) {
            $pi = $this->img_url($pic[0], 750, 400);
            array_push($p, $pi);
        }
        if (empty($p)) {
            $p[] = $this->img_url('');
        }

        $data['piclist'] = $p;
        //属性列表
        $data['attrlist'] = Model_Techan::product_attr($info['attrid']);
        //点评数
        $data['commentnum'] = Model_Comment::get_comment_num($info['id'], self::TYPE_ID);
        //销售数量
        $data['sellnum'] = Model_Member_Order::get_sell_num($info['id'], self::TYPE_ID) + intval($info['bookcount']);
        //产品编号
        $data['series'] = St_Product::product_series($info['id'], self::TYPE_ID);
        //产品图标
        $data['iconlist'] = $this->icons($info['iconlist']);
        //满意度
        $data['score'] = St_Functions::get_satisfy(self::TYPE_ID, $info['id'], $info['satisfyscore']);
        // 封面
        $data['litpic'] = $this->img_url($info['litpic']);
        //最低价
        $data['price'] = Model_Techan::get_minprice($info['id'], array('info' => $info));
        //会员折扣
        $discount_min_price = 0;
        if ($params['mid']) {
            $discount_min_price = Model_Api_V2_Discount::get_discount($params['mid'], self::TYPE_ID, $info['id'], $data['price']);
        }
        $data['discount_min_price'] = $discount_min_price;
        //市场价
        $data['sellprice'] = Model_Techan::get_min_sellprice($info['id']);
        //扩展字段信息
        $extend_info         = Model_Techan::extend($info['id'], self::TYPE_ID);
        $data['extend_info'] = $extend_info;
        // 获取特产内容栏目
        $params              = array(
            'typeid' => self::TYPE_ID,
            'productinfo' => $info,
            'onlyrealfield' => 1,
            'pc' => 0,

        );
        $data['detail_list'] = Model_Api_V2_Techan::get_content($params);
        // 积分信息
        $data['jifentprice_info']  = Model_Jifen_Price::get_used_jifentprice($info['jifentprice_id'], self::TYPE_ID);
        $data['jifenbook_info']    = Model_Jifen::get_used_jifenbook($info['jifenbook_id'], self::TYPE_ID);
        $data['jifencomment_info'] = Model_Jifen::get_used_jifencomment(self::TYPE_ID);
        // 套餐列表 详情页需要根据是否有套餐进行按钮处理
        $data['suit_list'] = Model_Api_V2_Techan::suit($info['id']);
        $field             = "id,productid,suitname,description,number,price";
        foreach ($data['suit_list'] as &$v) {
            //只返回需要的数据
            $v = Model_Api_V2_Common::res_data($field, $v);
        }
        //供应商
        if($info['supplierlist']) {
            $supplier_shop_service = new Service_Api_Supplier_Shop();
            $data['suppliers'] = $supplier_shop_service->get_shop_info($info['supplierlist']);
        }
        return $data;
    }

    public function query_condition(): array
    {
        $data = array(
            'sort' => array(
                array('id' => 0, 'name' => '综合排序'),
                array('id' => 1, 'name' => '价格从低到高'),
                array('id' => 2, 'name' => '价格从高到低'),
                array('id' => 3, 'name' => '销量优先'),
                array('id' => 4, 'name' => '人气推荐')
            ),
            'price' => array('name' => '价格', 'child' => array()),
        );

        // 属性
        $data['attribute'] = Model_Api_V2_Techan::get_attributes();
        //价格
        $data['price']['child'] = Model_Api_V2_Techan::price_filter();

        return $data;
    }

    public function create_order(array $params = array()): array
    {
        $mid         = $params['mid'];
        $member_info = $params['member_info'];

        // 创建订单数据
        $order = Model_Api_V2_Techan::make_order($params, $member_info);


        // 秒杀活动
        $seckill_id = Arr::get($params, 'seckill_id', 0);
        if ($seckill_id > 0) {
            $status = Model_Seckill::get_seckill_num($seckill_id, $mid);
            if (!$status) {
                throw new Exception('已超出活动限制次数');
            }
            $seckill                              = DB::select()
                ->from('seckill')
                ->where("id={$seckill_id}")
                ->execute()
                ->current();
            $seckill['suit_original_adult_price'] = $order['price'];
        }

        // 拼团
        $together_id = Arr::get($params, 'together_id', '');
        $join_id     = Arr::get($params, 'join_id', '');
        if ($together_id || $join_id) {
            $together_params      = array('join_id' => $join_id, 'ding_num' => $order['dingnum']);
            $check_order_together = Model_Api_V2_Together::check_order_together($together_id, $mid, $together_params);
            if (!$check_order_together['status']) {
                throw new Exception($check_order_together['msg']);
            }
            $together_info  = $check_order_together['data'];
            $together_price = $together_info['price'];
            $order['price'] = Currency_Tool::price($together_price['adult_price']);

            // 判断拼团是否允许使用会员折扣
            if (!in_array(Model_Together::ALLOW_DISCOUNT_TYPE_MEMBER_DISCOUNT, $together_info['discount_limit'])) {
                $params['use_member_discount'] = false;
            }
        }

        // 处理优惠券
        $coupon_id = Arr::get($params, 'couponid', '');
        if ($coupon_id) {
            $coupon_check = Model_Api_V2_Coupon::order_use_coupon_check($coupon_id, $mid, $order);
        }

        /*附加信息*/
        $extend_params = array(
            'member_card_id' => Arr::get($params, 'member_card_id', ''),
            'card_tourist_id' => Arr::get($params, 'card_tourist_id', ''),
        );

        // 秒杀不使用会员折扣
        if($seckill_id) {
            $extend_params['use_member_discount'] = false;
        }

        //添加订单
        $res = Model_Api_V2_Order::add_order($order, 'Model_Techan', $extend_params);

        if (!$res['status']) {
            throw new Exception($res['msg']);
        }
        $order_info = Model_Member_Order::get_order_by_ordersn($order['ordersn']);

        // 优惠券
        if (!empty($coupon_check)) {
            Model_Coupon::add_coupon_order($coupon_check['cid'], $order['ordersn'], $coupon_check['order_total_price'], $coupon_check, $coupon_id);
        }

        // 拼团
        if (isset($together_info)) {
            Model_Api_V2_Together::add_order($together_info, $order_info);
        }

        // 秒杀过来的就添加关联订单
        if (isset($seckill)) {
            Model_Seckill::add_seckill_order($order_info, $seckill);
        }

        // 添加红包
        if (St_Functions::is_normal_app_install('red_envelope')) {
            $envelope_member_id = Arr::get($params, 'envelope_member_id', '');
            if ($envelope_member_id && $mid) {
                Model_Order_Envelope::order_use_envelope($envelope_member_id, $order['ordersn'], self::TYPE_ID, $mid);
            }
        }

        // 订单结算信息表数据更新
        Model_Member_Order_Compute::update($order['ordersn']);


        // 预订事件
        Hook::trigger('order_created', $order);

        $filter_keys = array('id', 'orderid', 'ordersn', 'pageType', 'typeid', 'status');
        return $this->filter_data($filter_keys, $order_info);
    }

    use Trait_Api_Techan;
}